package com.demo.springcloud.controller;


import com.demo.springcloud.entity.CommonResult;
import com.demo.springcloud.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {
    public static final String USER_URL = "http://localhost:8001";
    @Resource
    private RestTemplate restTemplate;

    /**
     * 客户端添加用户
     *
     * @param user
     * @return
     */
    @GetMapping("/consumer/user/saveUser")
    public CommonResult<User> saveUser(User user) {
        return restTemplate.postForObject(USER_URL+"/user/saveUser",user,CommonResult.class);
    }

    @DeleteMapping("/consumer/user/deleteById/{id}")
    public CommonResult deleteById(@PathVariable("id") Long id) {
        restTemplate.delete("http://localhost:8001/user/saveUser"+id);
        return new CommonResult(200,"删除成功");
    }
    @GetMapping("/consumer/user/selectById/{id}")
    public CommonResult<User> selectById(@PathVariable("id") Long id){
        return restTemplate.getForObject(USER_URL+"/user/selectById"+id,CommonResult.class);
    }
}
